﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using ERF;
using FirstPerson.Objects;


namespace FirstPerson {
    public interface IScoreBoard { }
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class ScoreBoard : Microsoft.Xna.Framework.DrawableGameComponent, IScoreBoard {
        private Texture2D texture;
        private SpriteFont headLineFont;
        private SpriteFont scoreFont;
        protected PlayerHandler playerHandler;
        private SpriteBatch spriteBatch;

        public ScoreBoard(ERFGame game) : base(game) {
            //game.Services.AddService(typeof(IScoreBoard), this);
            this.playerHandler = (PlayerHandler)game.PlayerHandler;
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize() {
            this.Visible = false;
            base.Initialize();
        }

        public SpriteBatch SpriteBatch {
            get { return spriteBatch; }
            set { spriteBatch = value; }
        }

        protected override void LoadContent() {
            spriteBatch = new SpriteBatch(Game.GraphicsDevice);

            texture = Game.Content.Load<Texture2D>(@"Textures\ScoreBoard2");
            headLineFont = Game.Content.Load<SpriteFont>(@"Fonts2\FontHud");
            scoreFont = Game.Content.Load<SpriteFont>(@"Fonts2\menufont");

            base.LoadContent();
        }

        public override void Draw(GameTime gameTime) {
            spriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Deferred, SaveStateMode.SaveState);
            int w = (int)(Game.GraphicsDevice.Viewport.Width * 0.5 - texture.Width * 0.5), h = (int)(Game.GraphicsDevice.Viewport.Height * 0.5 - texture.Height * 0.5);

            spriteBatch.Draw(this.texture, this.GraphicsDevice.Viewport.TitleSafeArea, new Color(Color.White, 100));

            spriteBatch.DrawString(headLineFont, "Player", new Vector2(w + 100, h  + 60), Color.WhiteSmoke);
            spriteBatch.DrawString(headLineFont, "Kills", new Vector2(w + 400, h + 60), Color.WhiteSmoke);
            spriteBatch.DrawString(headLineFont, "Deaths", new Vector2(w + 500, h + 60), Color.WhiteSmoke);
            spriteBatch.DrawString(headLineFont, "Ping", new Vector2(w + 600, h + 60), Color.WhiteSmoke);

            int i = 0;
            foreach (Player p in this.playerHandler.PlayersList) {
                spriteBatch.DrawString(scoreFont, p.Name, new Vector2(w + 100, h + 35 * (i + 3)), Color.Black);
                spriteBatch.DrawString(scoreFont, p.Kills.ToString(), new Vector2(w + 400, h + 35 * (i + 3)), Color.Black);
                spriteBatch.DrawString(scoreFont, p.Deaths.ToString(), new Vector2(w + 500, h + 35 * (i + 3)), Color.Black);
                spriteBatch.DrawString(scoreFont, ((NetworkPlayer)p).NetworkGamer.RoundtripTime.TotalMilliseconds.ToString(), new Vector2(w + 600, h + 35 * (i + 3)), Color.Black);
                i++;
            }

            spriteBatch.End();
            base.Draw(gameTime);
        }
    }
}